package com.wangyadong.hobby.schedule.jobtask.ath.lesson1.stackqueue;

import org.springframework.util.Assert;

import java.util.Iterator;
import java.util.function.Consumer;

/**
 * <p>
 *
 * @author <714037465@qq.com>
 * @since 2019/4/1717:43
 **/
public class StackExcample<T> implements Iterator {

    private Node<T> first;
    private int N;


    public int size() {
        return N;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    public void push(T t) {
        Node node = null;
        if (first == null) {
            node = new Node(null, t);
            first = node;
        } else {
            Node old = first;
            Node<T> tNode = new Node<T>();
            tNode.setT(t);
            tNode.setNode(old);
            first = tNode;
        }
        N++;
    }

    public T pop() {
        if (first == null) {
            return null;
        }
        Node node = first.getNode();
        T t = first.getT();
        first = node;
        N--;
        return t;
    }

    private class Node<T> {
        private Node node;
        private T t;

        public Node(Node node, T t) {
            this.node = node;
            this.t = t;
        }

        public Node() {
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node) {
            this.node = node;
        }

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void forEachRemaining(Consumer action) {

    }

    @Override
    public boolean hasNext() {
        return first.getNode() != null;
    }

    @Override
    public Object next() {
        return pop();
    }

    /**
     *  [()]{}{[()()]()}
     * @param args
     */
    public static void main(String[] args) {
        StackExcample<String> stack = new StackExcample<>();
        stack.push("0");
        stack.push("1");
        stack.push("2");
        System.out.println(stack.size());
        String pop = stack.pop();
        System.out.println("结果应该是0" + pop);
        System.out.println(stack.size());
        pop = stack.pop();
        System.out.println("结果应该是1" + pop);
        System.out.println(stack);
    }
}
